
package com.android.sharecar.activity;

import com.android.sharecar.model.ReservationCycle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TimePicker;

import java.util.Calendar;

public class ReservationCyleActivity extends Activity {

    private CheckBox mEveryYear, mEveryMonth, mEveryDay;

    private CheckBox mWorkingDay, mNonWorkingDay, mMondayToFriday;

    private CheckBox mMorning, mAfternoon;

    private CheckBox mExceptionalDay_Holiday, mExceptionalDay_Weekend;

    private TimePicker mWhatClock;

    private Button mPositive, mCancel;

    private String mTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reservation_cycle);

        findViews();
        setListenerForViews();

        Calendar calendar = Calendar.getInstance();
        mWhatClock.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        mWhatClock.setCurrentMinute(calendar.get(Calendar.MINUTE));
        mWhatClock.setIs24HourView(true);
    }

    private void findViews() {
        mAfternoon = (CheckBox) findViewById(R.id.reservation_cb_afternoon);
        mEveryDay = (CheckBox) findViewById(R.id.reservation_cb_erveryDay);
        mEveryMonth = (CheckBox) findViewById(R.id.reservation_cb_erveryMonth);
        mEveryYear = (CheckBox) findViewById(R.id.reservation_cb_erveryYear);
        mExceptionalDay_Holiday = (CheckBox) findViewById(R.id.reservation_cb_holiday);
        mExceptionalDay_Weekend = (CheckBox) findViewById(R.id.reservation_cb_weekend);
        mMorning = (CheckBox) findViewById(R.id.reservation_cb_morning);
        mNonWorkingDay = (CheckBox) findViewById(R.id.reservation_cb_nonWorkingDay);
        mMondayToFriday = (CheckBox) findViewById(R.id.reservation_cb_mondayToFriday);
        mWorkingDay = (CheckBox) findViewById(R.id.reservation_cb_workingDay);
        mWhatClock = (TimePicker) findViewById(R.id.reservation_tp_whatClock);
        mPositive = (Button) findViewById(R.id.reservation_btn_positive);
        mCancel = (Button) findViewById(R.id.reservation_btn_cancel);
    }

    private void setListenerForViews() {
        mWhatClock.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                mTime = "";
                mTime = hourOfDay + ":" + minute;
            }
        });

        mPositive.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                getUserChoicesAndReturnResult();
            }

        });

        mCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ReservationCyleActivity.this.setResult(RESULT_CANCELED);
                ReservationCyleActivity.this.finish();
            }
        });
    }

    /**
     * Get user's choices and return the result to the activity which started
     * this activity.
     */
    private void getUserChoicesAndReturnResult() {
        Intent resultIntent = new Intent();
        Bundle resultBundle = new Bundle();
        ReservationCycle cycle = new ReservationCycle();

        if (mAfternoon.isChecked()) {
            cycle.setAfternoon(true);
        }
        if (mEveryDay.isChecked()) {
            cycle.setEveryDay(true);
        }
        if (mEveryMonth.isChecked()) {
            cycle.setEveryMonth(true);
        }
        if (mEveryYear.isChecked()) {
            cycle.setEveryYear(true);
        }
        if (mExceptionalDay_Holiday.isChecked()) {
            cycle.setExceptional_holiday(true);
        }
        if (mExceptionalDay_Weekend.isChecked()) {
            cycle.setExceptional_weekend(true);
        }
        if (mMondayToFriday.isChecked()) {
            cycle.setMondayToFriday(true);
        }
        if (mMorning.isChecked()) {
            cycle.setMorning(true);
        }
        if (mNonWorkingDay.isChecked()) {
            cycle.setNonWorkingDay(true);
        }
        if (mWorkingDay.isChecked()) {
            cycle.setWorkingDay(true);
        }
        if (!mTime.equals("")) {
            cycle.setWhatClock(true);
            cycle.setWhatClock(mTime);
        }

        resultBundle.putSerializable(ReservationCycle.TAG, cycle);

        // Set the result.
        resultIntent.putExtras(resultBundle);
        this.setResult(RESULT_OK, resultIntent);
        this.finish();
    }
}
